package io.gitee.hefren.hhmq.store;

import com.google.common.collect.Maps;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Map;

/**
 * @Date 2024/7/31
 * @Author lifei
 */
public class HhIndexer {

    private static final MultiValueMap<String, Entry> topicEntriesMap = new LinkedMultiValueMap<>();
    private static final Map<String, Map<Integer, Entry>> topicOffsetMap = Maps.newHashMap();

    public static class Entry{
        private int offset;
        private int len;

        public Entry() {
        }

        public Entry(int offset, int len) {
            this.offset = offset;
            this.len = len;
        }

        public int getOffset() {
            return offset;
        }

        public void setOffset(int offset) {
            this.offset = offset;
        }

        public int getLen() {
            return len;
        }

        public void setLen(int len) {
            this.len = len;
        }
    }


    public static void addEntry(String topic, int offset, int len) {
        Entry entry = new Entry(offset, len);
        topicEntriesMap.add(topic, entry);
        topicOffsetMap.putIfAbsent(topic, Maps.newHashMap());
        Map<Integer, Entry> offsetMap = topicOffsetMap.get(topic);
        offsetMap.put(offset, entry);
    }


    public static Entry getEntry(String topic, int offset) {
        if (!topicOffsetMap.containsKey(topic)) {
            return null;
        }
        return topicOffsetMap.get(topic).get(offset);
    }


}
